package com.jcl.recruit.timer.crawler.service.impl;

import com.google.gson.Gson;
import com.jcl.recruit.resume.model.ResumeOrig;
import com.jcl.recruit.searchresume.model.SearchResume;
import com.jcl.recruit.searchresume.model.SearchResumeEdu;
import com.jcl.recruit.searchresume.model.SearchResumeProject;
import com.jcl.recruit.searchresume.model.SearchResumeWork;
import com.jcl.recruit.timer.crawler.bean.JxBean;
import com.jcl.recruit.timer.crawler.bean.JxResumeBean;
import com.jcl.recruit.timer.crawler.bean.LiepinJsonBean;
import com.jcl.recruit.timer.crawler.service.CrawlerChannelResumeJx;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName crawlerChannelResumeTaskService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/30 15:57
 * @Version 1.0
 **/
public class HunterCrawlerChannelResume implements CrawlerChannelResumeJx {


    @Override
    public JxBean crawlerResume(List<ResumeOrig> resumeOrigList) throws Exception{
        JxBean jxBean = new JxBean();
        List<JxResumeBean> jxResumeBeans = new ArrayList<>();
        jxBean.setJxResumeBeans(jxResumeBeans);

        for(ResumeOrig orig: resumeOrigList) {

            jxBean.setExternalId(orig.getId());
            JxResumeBean jxResumeBean = new JxResumeBean();
            jxResumeBeans.add(jxResumeBean);

            String str = orig.getContent();
            String json = str.substring(0, str.indexOf("}") + 1);
            String html = str.substring(str.indexOf("<div class=\"wrap relative\" data-selector=\"wrap\">"), str.lastIndexOf("<p class=\"back-top\""));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            SearchResume searchResume = new SearchResume();
            Document doc = Jsoup.parse(html);
            Elements elements = doc.select("td.clearfix");
            for (Element entity : elements) {
                // 获取当前年薪期望年薪以及当前城市信息
                if (entity.text().contains("目前年薪")) {
                    searchResume.setCurrentSalary(entity.text().substring(entity.text().indexOf("目前年薪：") + 5, entity.text().indexOf("期望年薪")).trim());
                    String desireSalary = entity.text().substring(entity.text().indexOf("期望年薪：") + 5);
                    if (!desireSalary.equals("面议") && !desireSalary.equals("未填") && !desireSalary.equals("保密")) {
                        searchResume.setDesireSalaryStart(((Float) (Float.valueOf(desireSalary.substring(0, desireSalary.indexOf("万"))) * 10000)).intValue());
                    }
                    ;
                } else if (entity.text().contains("目前地点")) {
                    searchResume.setCurrentAddress(entity.text().substring(entity.text().indexOf("目前地点：") + 5, entity.text().indexOf("期望地点")).trim());
                    searchResume.setCurrentCity(searchResume.getCurrentAddress());
                }
            }
            String regexNumber = "[^0-9]";
            //json 数据解析获取用户名，性别工作年限以及学历
            LiepinJsonBean liepinJsonBean = new Gson().fromJson(json, LiepinJsonBean.class);
            searchResume.setRealname(liepinJsonBean.getResName());
            searchResume.setSex(liepinJsonBean.getResSex());
            searchResume.setEduLastEducation(liepinJsonBean.getResEdulevelName());
            searchResume.setWorkyear(Integer.valueOf(liepinJsonBean.getWorkYearsShow().replaceAll(regexNumber, "")));
            if (StringUtils.isNotBlank(liepinJsonBean.getAgeShow())) {
                Integer birthYear = Integer.valueOf(sdf.format(new Date())) - Integer.valueOf(liepinJsonBean.getAgeShow().replaceAll(regexNumber, ""));
                searchResume.setBirthday(sdf.parse(birthYear.toString(), new ParsePosition(0)));
            }
            Elements selfEvaluation = doc.select("p.attach-info-cont");
            searchResume.setEvaluate(selfEvaluation.text());
            // 工作经历
            Elements workTitles = doc.select("section.work-experience");
            Elements works = workTitles.eq(0).select("div.work-cont");
            List<SearchResumeWork> resumeWorks = new ArrayList<>();
            for (Element work : works) {
                SearchResumeWork searchResumeWork = new SearchResumeWork();
                /*公司*/
                searchResumeWork.setCompany(work.select("span.filter-zone").eq(0).text());
                /*职位简介*/
                searchResumeWork.setDuty(work.select("span.filter-zone").eq(1).text());
                /*时间*/
                String times = work.select("p.filter-zone").eq(0).text();
                searchResumeWork.setStartDate(times.substring(0, times.indexOf("-")));
                searchResumeWork.setEndDate(times.substring(times.indexOf("-") + 1, times.indexOf("(")));
                /*职位名称*/
                searchResumeWork.setPosition(work.select("p.filter-zone").eq(1).text());
                //searchResumeWork.setResumeId(searchResume.getId());
                resumeWorks.add(searchResumeWork);
            }
            // 教育经历
            Elements eduTitle = doc.select("section.education-experience");
            Elements edus = eduTitle.eq(0).select("li.clearfix");
            List<SearchResumeEdu> searchResumeEdus = new ArrayList<>();
            eduFor:
            for (Element edu : edus) {
                SearchResumeEdu searchResumeEdu = new SearchResumeEdu();
                String[] strings = edu.select("p>span").text().split("·");
                if (strings.length < 2) {
                    continue eduFor;
                }
                //searchResumeEdu.setResumeId(searchResume.getId());
                searchResumeEdu.setSchool(strings[0].trim());
                searchResumeEdu.setMajor(strings[1].trim());
                searchResumeEdu.setDegree(strings[2].trim());
                String times = edu.select("time.text-primary").text();
                searchResumeEdu.setStartDate(times.substring(0, times.indexOf("-")));
                searchResumeEdu.setEndDate(times.substring(times.indexOf("-") + 1));
                searchResumeEdus.add(searchResumeEdu);
            }
            if (resumeWorks.size() > 0) {
                searchResume.setWorkLastCompany(resumeWorks.get(0).getCompany());
                searchResume.setWorkLastPosition(resumeWorks.get(0).getPosition());
                searchResume.setWorkDetail(resumeWorks.get(0).getDuty());
            }
            if (searchResumeEdus.size() > 0) {
                searchResume.setEduLastSchool(searchResumeEdus.get(0).getSchool());
                searchResume.setEduLastMajor(searchResumeEdus.get(0).getMajor());
            }
            searchResume.setSourceSub("liepin");
            searchResume.setStatus(0);
            searchResume.setApplyStatus(0);
            searchResume.setSource(0);
            searchResume.setCreatetime(new Date());
            searchResume.setUpdateDate(searchResume.getCreatetime());
            searchResume.setKeyword(orig.getKeyword());

            jxResumeBean.setSearchResume(searchResume);


            // 项目经历
            Elements projectTitle = doc.select("section.project-experience");
            Elements projects = projectTitle.eq(0).select("div.project-cont");
            List<SearchResumeProject> resumeProjects = new ArrayList<>();
            for (Element project : projects) {
                SearchResumeProject searchResumeProject = new SearchResumeProject();
                // 项目名称
                searchResumeProject.setProjectname(project.select("span.filter-zone").eq(0).text());
                // 项目指责
                searchResumeProject.setDuty(project.select("span.filter-zone").eq(2).text());
                // 项目描述
                searchResumeProject.setDescription(project.select("p.filter-zone").eq(0).text());
                // 时间
                String times = project.select("span.text-right").eq(0).text();
                searchResumeProject.setStartDate(times.substring(0, times.indexOf("-")));
                searchResumeProject.setEndDate(times.substring(times.indexOf("-") + 1, times.indexOf("(")));
                resumeProjects.add(searchResumeProject);
            }

            jxResumeBean.setSearchResumeEdus(searchResumeEdus);
            jxResumeBean.setResumeWorks(resumeWorks);
            jxResumeBean.setResumeProjects(resumeProjects);

        }

        return jxBean;
    }
}
